package com.rjt.article.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rjt.article.model.ArticleCommentModel;
import com.rjt.article.model.ArticleModel;
import com.rjt.article.service.ArticleCircleShareService;
import com.rjt.article.service.ArticleCommentService;
import com.rjt.article.service.ArticleFriendsShareService;
import com.rjt.article.service.ArticleService;
import com.rjt.article.service.ArticleVisitService;
import com.rjt.article.vo.ArticleVisitVo;
import com.rjt.article.vo.ArticleVo;
import com.rjt.permission.Permission;

import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;


@Controller
@RequestMapping("article")
public class ArticleController {
	
	@Value("${wechat.oauthUrl}")
	private String oauthUrl;

	@Autowired
	private WxMpService wxMpService;
	
	@Autowired
	private ArticleService articleService;
	
	@Autowired
	private ArticleCommentService articleCommentService;
	
	@Autowired
	private ArticleVisitService articleVisitService;
	
	@Autowired
	private ArticleFriendsShareService articleFriendsShareService;
	
	@Autowired
	private ArticleCircleShareService articleCircleShareService;
	
	
	@Value("${UEditor.picPath}")
	private String picPath;
	@Value("${UEditor.hostUrl}")
	private String hostUrl;
	
	@GetMapping("getUserInfo")
	public String getUserInfo(String code,HttpServletResponse response,String url) {
		WxMpOAuth2AccessToken wxMpOAuth2AccessToken;
		try {
			wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
			boolean valid = wxMpService.oauth2validateAccessToken(wxMpOAuth2AccessToken);
			if (!valid) {
				wxMpOAuth2AccessToken = wxMpService.oauth2refreshAccessToken(wxMpOAuth2AccessToken.getRefreshToken());
			}
			setCookieAndSession(response, wxMpOAuth2AccessToken);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("异常");
		}	
		return "redirect:" + url;

	}

	private void setCookieAndSession(HttpServletResponse response, 
			WxMpOAuth2AccessToken wxMpOAuth2AccessToken) throws WxErrorException, UnsupportedEncodingException {
		WxMpUser wxMpUser = wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken, null);
		Cookie cookie=new Cookie("headimgurl", wxMpUser.getHeadImgUrl());
		Cookie cookie2=new Cookie("nickname", URLEncoder.encode(wxMpUser.getNickname(), "UTF-8"));
		cookie.setDomain(oauthUrl.replace("http://", ""));
		cookie.setPath("/");
		cookie.setMaxAge(365 * 24 * 60 * 60);
		cookie2.setDomain(oauthUrl.replace("http://", ""));
		cookie2.setPath("/");
		cookie2.setMaxAge(365 * 24 * 60 * 60);
		response.addCookie(cookie);
		response.addCookie(cookie2);
	}
	
	@GetMapping("toNewPage")
	public String toNewPage() {
		return "weixin/add";
	}
	
	@PostMapping("addArticle")
	@ResponseBody
	@Permission
	public String addArticle(@RequestBody ArticleVo articleVo) {
		return articleService.add(articleVo,"save");
	}
	
	@GetMapping("list")
	@Permission
	public String list() {
		return "weixin/list";
	}
	@ResponseBody
	@GetMapping("articleList")
	public Page<ArticleModel> articleList(HttpServletRequest request) {
		return articleService.getList(request);
	}
	
	@ResponseBody
	@PostMapping("commentAdd")
	public String commentAdd(String content,HttpSession session,String id,HttpServletRequest request) {
		return articleCommentService.save(content,session,id,request);
	}
	
	@ResponseBody
	@PostMapping("delArticle")
	@Permission
	public String delArticle(@RequestBody List<ArticleModel> articles) {
		return articleService.delete(articles);
	}
	
	@GetMapping("toUpdatePage")
	public String toUpdatePage(String id,HttpServletRequest request) {
		ArticleVo articleVo = articleService.findArticle(id);
		request.setAttribute("article",articleVo);
		return "weixin/edit";
	}
	
	@PostMapping("editArticle")
	@ResponseBody
	@Permission
	public String editArticle(@RequestBody ArticleVo articleVo) {
		return articleService.add(articleVo,"edit");
	}
	
	@ResponseBody
	@GetMapping("commentList")
	@Permission
	public Page<ArticleCommentModel> commentList(HttpServletRequest request) {
		return articleCommentService.getList(request);
	}
	
	@GetMapping("toCommentList")
	public String tocommentList(String id,HttpServletRequest request) {
		request.setAttribute("rId",id);
		return "weixin/commentList";
	}
	
	@ResponseBody
	@PostMapping("delCommnet")
	@Permission
	public void delArticle(int id) {
		 articleCommentService.delete(id);
	}
	
	@GetMapping("toUpdateCommnet")
	public String toUpdateCommnet(Integer id,HttpServletRequest request) {
		ArticleCommentModel articleComment = articleCommentService.findOne(id);
		request.setAttribute("articleComment",articleComment);
		return "weixin/commentEdit";
	}
	
	@PostMapping("editArticleComment")
	@ResponseBody
	@Permission
	public String editArticleComment(@RequestBody ArticleCommentModel articleComment) {
		return articleCommentService.update(articleComment);
	}
	
	@GetMapping("countByArea")
	@ResponseBody
	@Permission
	public List<ArticleVisitVo> countByArea(String rId) {
		return articleVisitService.countByRIdAndArea(rId);
	}
	
	@GetMapping("countByDate")
	@ResponseBody
	@Permission
	public List<ArticleVisitVo> countByDate(String rId) {
		return articleVisitService.countByRIdAndDate(rId);
	}
	
	@GetMapping("toCountPage")
	public String toCountPage(String id,HttpServletRequest req) {
		req.setAttribute("rId", id);
		return "weixin/count";
	}
	
	@PostMapping("parseUrl")
	@ResponseBody
	@Permission
	public ArticleVo parseUrl(String weixinUrl) {
		return articleService.parseUrl(weixinUrl);
	}
	
	@GetMapping("countFriendsByArea")
	@ResponseBody
	@Permission
	public List<ArticleVisitVo> countFriendsByArea(String rId) {
		return articleFriendsShareService.countByRIdAndArea(rId);
	}
	
	@GetMapping("countFriendsByDate")
	@ResponseBody
	@Permission
	public List<ArticleVisitVo> countFriendsByDate(String rId) {
		return articleFriendsShareService.countByRIdAndDate(rId);
	}
	
	@GetMapping("toCountFriendsPage")
	public String toCountFriendsPage(String id,HttpServletRequest req) {
		req.setAttribute("rId", id);
		return "weixin/countFriends";
	}
	
	@GetMapping("countCircleByArea")
	@ResponseBody
	@Permission
	public List<ArticleVisitVo> countCircleByArea(String rId) {
		return articleCircleShareService.countByRIdAndArea(rId);
	}
	
	@GetMapping("countCircleByDate")
	@ResponseBody
	@Permission
	public List<ArticleVisitVo> countCircleByDate(String rId) {
		return articleCircleShareService.countByRIdAndDate(rId);
	}
	
	@GetMapping("toCountCirclePage")
	public String toCountCirclePage(String id,HttpServletRequest req) {
		req.setAttribute("rId", id);
		return "weixin/countCircle";
	}
}
